package org.xshop.web.biz;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xshop.core.JPushHelper;
import org.xshop.model.TActivity;
import org.xshop.service.ITActivityService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 活动  前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-04-08
 */
@RestController
@RequestMapping("/activity")
@Api(value = "活动接口", description = "活动接口")
public class TActivityController extends BaseController<TActivity, ITActivityService> {
    @Autowired
    private JPushHelper jpushHelper;
    @RequiresPermissions("activity.read")
    @PutMapping(value = "/read/list")
    @ApiOperation(value = "查询活动", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object query(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        return super.query(param);
    }

    @RequiresPermissions("activity.read")
    @PutMapping(value = "/read/detail")
    @ApiOperation(value = "活动详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object get(HttpServletRequest request) {
        TActivity param = WebUtil.getParameter(request, TActivity.class);
        return super.get(param);
    }

    @PostMapping
    @RequiresPermissions("activity.update")
    @ApiOperation(value = "修改活动", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request) {
        TActivity param = WebUtil.getParameter(request, TActivity.class);
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("type", "1");
        jpushHelper.aliasMessageAll(param.getActivityName(), "活动通知", hashMap);
        return super.update(param);
    }

    @DeleteMapping
    @RequiresPermissions("activity.delete")
    @ApiOperation(value = "删除活动", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object del(HttpServletRequest request) {
        TActivity param = WebUtil.getParameter(request, TActivity.class);
        Assert.notNull(param.getId(), "ID不能为空");
        return super.delete(param);
    }
}
